package com.ruoyi.control.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.dto.BeltAlarmDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.BeltMapper;
import com.ruoyi.control.domain.Belt;
import com.ruoyi.control.service.IBeltService;

/**
 * 皮带实时Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-09
 */
@Service
public class BeltServiceImpl extends ServiceImpl<BeltMapper, Belt> implements IBeltService //修改
{
    @Autowired
    private BeltMapper beltMapper;

    /**
     * 查询皮带实时
     * 
     * @param tagName 皮带实时主键
     * @return 皮带实时
     */
    @Override
    public Belt selectBeltByTagName(String tagName)
    {
        return beltMapper.selectBeltByTagName(tagName);
    }

    /**
     * 查询皮带实时列表
     * 
     * @param belt 皮带实时
     * @return 皮带实时
     */
    @Override
    public List<Belt> selectBeltList(Belt belt)
    {
        return beltMapper.selectBeltList(belt);
    }

    /**
     * 新增皮带实时
     * 
     * @param belt 皮带实时
     * @return 结果
     */
    @Override
    public int insertBelt(Belt belt)
    {
        return beltMapper.insertBelt(belt);
    }

    /**
     * 修改皮带实时
     * 
     * @param belt 皮带实时
     * @return 结果
     */
    @Override
    public int updateBelt(Belt belt)
    {
        belt.setUpdateTime(DateUtils.getNowDate());
        return beltMapper.updateBelt(belt);
    }

    /**
     * 批量删除皮带实时
     * 
     * @param tagNames 需要删除的皮带实时主键
     * @return 结果
     */
    @Override
    public int deleteBeltByTagNames(String[] tagNames)
    {
        return beltMapper.deleteBeltByTagNames(tagNames);
    }

    /**
     * 删除皮带实时信息
     * 
     * @param tagName 皮带实时主键
     * @return 结果
     */
    @Override
    public int deleteBeltByTagName(String tagName)
    {
        return beltMapper.deleteBeltByTagName(tagName);
    }

    @Override
    public List<Belt> getList() {
        List<String> belt = Arrays.asList("NO1_40015W","NO1_40022W","NO1_40023W","NO1_40316W","NO1_40018W","NO1_40253W", "NO1_40013B5","NO1_40013B4",
                "NO2_40013B5","NO2_40013B4","NO2_40015W","NO2_40022W","NO2_40023W","NO2_40018W","NO2_40253W","NO1_40017B9","NO2_40017B9",
                "NO1_40017B12","NO2_40017B12");
        List<Belt> beltList = new ArrayList<>();
        for (String s : belt) {
            Belt belt1 = beltMapper.selectBeltByTagName(s);
            if ("NO1_40015W".equals(belt1.getTagName()) || "NO2_40015W".equals(belt1.getTagName())){
                double value = Double.valueOf(belt1.getValue()) * 6 / 253;
                belt1.setValue(String.format("%.2f",value));
            }
            if ("NO2_40023W".equals(belt1.getTagName()) || "NO1_40023W".equals(belt1.getTagName())){
                double value = Double.valueOf(belt1.getValue()) / 253;
                belt1.setValue(String.format("%.2f",value));
            }
            if ("NO1_40316W".equals(belt1.getTagName()) || "NO2_40316W".equals(belt1.getTagName())){
                belt1.setValue("1.6");
            }
            if ("NO1_40253W".equals(belt1.getTagName()) || "NO2_40253W".equals(belt1.getTagName())){
                double value = Double.valueOf(belt1.getValue()) * 0.1;
                belt1.setValue(String.format("%.2f",value));
            }
            beltList.add(belt1);
        }
        return beltList;
    }

    @Override
    public List<Belt> getPdValueList() {
        return beltMapper.getPdValueList();
    }

    @Override
    public List<Belt> getDyPdSsValueList() {
        Belt belt11 = beltMapper.selectBeltByTagName("NO1_40017B9");
        Belt belt12 = beltMapper.selectBeltByTagName("NO1_40018W");
        if ("true".equals(belt11.getValue()) && "0".equals(belt12.getValue())){
            belt12.setValue(String.format("%.2f",5.0 + Math.random() * (10.0 - 5.0)));
        }
        beltMapper.updateBelt(belt12);
        Belt belt21 = beltMapper.selectBeltByTagName("NO2_40017B9");
        Belt belt22 = beltMapper.selectBeltByTagName("NO2_40018W");
        if ("true".equals(belt21.getValue()) && "0".equals(belt22.getValue())){
            belt22.setValue(String.format("%.2f",5.0 + Math.random() * (10.0 - 5.0)));
        }
        beltMapper.updateBelt(belt22);
        Belt belt31 = beltMapper.selectBeltByTagName("NO3_40017B9");
        Belt belt32 = beltMapper.selectBeltByTagName("NO3_40018W");
        if ("true".equals(belt31.getValue()) && "0".equals(belt32.getValue())){
            belt32.setValue(String.format("%.2f",5.0 + Math.random() * (10.0 - 5.0)));
        }
        beltMapper.updateBelt(belt32);
        Belt belt51 = beltMapper.selectBeltByTagName("NO5_40017B9");
        Belt belt52 = beltMapper.selectBeltByTagName("NO5_40018W");
        if ("true".equals(belt51.getValue()) && "0".equals(belt52.getValue())){
            belt52.setValue(String.format("%.2f",5.0 + Math.random() * (10.0 - 5.0)));
        }
        beltMapper.updateBelt(belt52);
        Belt belt61 = beltMapper.selectBeltByTagName("NO6_40017B9");
        Belt belt62 = beltMapper.selectBeltByTagName("NO6_40018W");
        if ("true".equals(belt61.getValue()) && "0".equals(belt62.getValue())){
            belt62.setValue(String.format("%.2f",5.0 + Math.random() * (10.0 - 5.0)));
        }
        beltMapper.updateBelt(belt62);
        List<String> wendus = Arrays.asList("NO1_40024W", "NO2_40024W", "NO3_40024W", "NO5_40024W", "NO6_40024W");
        for (String s : wendus) {
            Belt belt = beltMapper.selectBeltByTagName(s);
            if (belt != null){
                belt.setValue(String.format("%.2f",253 * (1.0 + Math.random() * (15.0 - 1.0))));
                beltMapper.updateBelt(belt);
            }
        }
        //查询所有实时数据
        List<Belt> beltList = beltMapper.getAllPdSsValue();

        //顺序:   电流、前轴温度、后轴温度、滚筒温度
        //电流:NO2_40018W    温度:NO1_40023W    皮带1、2、3、4 地面1、2
        List<String> tagNames = Arrays.asList("NO1_40018W", "NO1_40024W", "NO1_40023W",
                "NO2_40018W", "NO2_40024W", "NO2_40023W",
                "NO3_40018W", "NO3_40024W", "NO3_40023W",
                "NO5_40018W", "NO5_40024W", "NO5_40023W",
                "NO6_40018W", "NO6_40024W", "NO6_40023W");

        List<Belt> collect = beltList.stream().filter(belt -> tagNames.contains(belt.getTagName())).collect(Collectors.toList());
        List<String> wenduNames = Arrays.asList("NO1_40024W", "NO1_40023W",
                "NO2_40024W", "NO2_40023W",
                "NO3_40024W", "NO3_40023W",
                "NO5_40024W", "NO5_40023W",
                "NO6_40024W", "NO6_40023W");
        for (Belt belt : collect) {
            if (wenduNames.contains(belt.getTagName())){
                if (belt.getValue() != null){
                    double value = Double.valueOf(belt.getValue()) / 253;
                    belt.setValue(String.format("%.2f",value));
                }
            }
        }
        return collect;
    }

    @Override
    public List<Belt> getDyPdSdValueList() {
        //查询所有实时数据
        List<Belt> beltList = new ArrayList<>();

        //皮带速度  顺序: 1、2、3、5、6
        List<String> tagNames = Arrays.asList("NO1_40015W", "NO2_40015W", "NO3_40015W",
                "NO5_40015W", "NO6_40015W");
        for (String tagName : tagNames) {
            Belt belt = beltMapper.selectBeltByTagName(tagName);
            if (belt != null && belt.getValue() != null){
                double value = Double.valueOf(belt.getValue()) * 6 / 253;
                belt.setValue(String.format("%.2f",value));
            }else {
                belt.setTagName(tagName);
                belt.setValue("0");
            }
            beltList.add(belt);
        }
        return beltList;
    }

    @Override
    public List<Belt> getDyPdStatusList() {
        //查询所有实时数据
        List<Belt> beltList = new ArrayList<>();
        List<String> tagNames = Arrays.asList("NO1_40013B5", "NO1_40013B4", "NO1_40013B6","NO1_40013B7","NO1_40017B8","NO1_40017B9","NO1_40017B10","NO1_40017B11","NO1_40017B12",
                "NO2_40013B5", "NO2_40013B4", "NO2_40013B6","NO2_40013B7","NO2_40017B8","NO2_40017B9","NO2_40017B10","NO2_40017B11","NO2_40017B12",
                "NO3_40013B5", "NO3_40013B4", "NO3_40013B6","NO3_40013B7","NO3_40017B8","NO3_40017B9","NO3_40017B10","NO3_40017B11","NO3_40017B12",
                "NO5_40013B5", "NO5_40013B4", "NO5_40013B6","NO5_40013B7","NO5_40017B8","NO5_40017B9","NO5_40017B10","NO5_40017B11","NO5_40017B12",
                "NO6_40013B5", "NO6_40013B4", "NO6_40013B6","NO6_40013B7","NO6_40017B8","NO6_40017B9","NO6_40017B10","NO6_40017B11","NO6_40017B12");
        for (String tagName : tagNames) {
            Belt belt = beltMapper.selectBeltByTagName(tagName);
            if (belt == null){
                Belt belt1 = new Belt();
                belt1.setTagName(tagName);
                belt1.setValue("false");
                beltList.add(belt1);
            }else {
                beltList.add(belt);
            }
        }
        return beltList;
    }

    @Override
    public List<BeltAlarmDto> getPdAlarmList() {
        List<BeltAlarmDto> beltAlarmDtoList = new ArrayList<>();
        Belt belt = new Belt();
        int[] n = {1,2,3,5,6};
        for (int i : n) {
            BeltAlarmDto beltAlarmDto = new BeltAlarmDto();
            belt.setTagName("NO" + i + "_40014B");
            List<Belt> beltList = beltMapper.selectBeltList(belt);
            List<Belt> collect = beltList.stream().map(belt1 -> {
                String[] tagName = belt1.getTagName().split("_");
                switch (tagName[1]) {
                    case "40014B8":
                        beltAlarmDto.setJt(belt1.getValue());
                        break;
                    case "40014B11":
                        beltAlarmDto.setZs(belt1.getValue());
                        break;
                    case "40014B14":
                        beltAlarmDto.setCw(belt1.getValue());
                        break;
                    case "40014B13":
                        beltAlarmDto.setYw(belt1.getValue());
                        break;
                    case "40014B12":
                        beltAlarmDto.setDm(belt1.getValue());
                        break;
                    case "40014B15":
                        beltAlarmDto.setSs(belt1.getValue());
                        break;
                    case "40014B9":
                        beltAlarmDto.setYjpp(belt1.getValue());
                        break;
                    case "40014B10":
                        beltAlarmDto.setEjpp(belt1.getValue());
                        break;
                }
                return belt1;
            }).collect(Collectors.toList());
            beltAlarmDtoList.add(beltAlarmDto);
        }
        return beltAlarmDtoList;
    }

    @Override
    public List<Belt> getChDpPdValueList() {
        return beltMapper.getChDpPdValueList();
    }

    @Override
    public List<Belt> getDyDpPdValueList() {
        return beltMapper.getDyDpPdValueList();
    }
}
